<template>
  <div>
    <div class="gva-form-box">
      <el-form :model="formData" ref="elFormRef" label-position="right" :rules="rule" label-width="80px">
        <el-form-item label="接警时间:" prop="touchAt">
          <el-date-picker v-model="formData.touchAt" type="date" placeholder="选择日期" :clearable="true"></el-date-picker>
        </el-form-item>
        <el-form-item label="案件编号:" prop="caseNo">
          <el-input v-model="formData.caseNo" :clearable="true" placeholder="请输入案件编号"/>
        </el-form-item>
        <el-form-item label="案件名:" prop="caseName">
          <el-input v-model="formData.caseName" :clearable="true" placeholder="请输入案件名"/>
        </el-form-item>
        <el-form-item label="发案乡镇:" prop="townCode">
          <el-select v-model="formData.townCode" placeholder="请选择发案乡镇" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in townOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="主办人:" prop="fOwnerId">
          <el-select v-model="formData.fOwnerId" placeholder="请选择主办人" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in dataSource.fOwnerId" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="金额:" prop="amount">
          <el-input v-model.number="formData.amount" :clearable="true" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="案件状态:" prop="caseState">
          <el-select v-model="formData.caseState" placeholder="请选择案件状态" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in case_stateOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="案件类别:" prop="fCaseType">
          <el-select v-model="formData.fCaseType" placeholder="请选择案件类别" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in f_case_typeOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="副案件类别:" prop="sCaseType">
          <el-select v-model="formData.sCaseType" placeholder="请选择副案件类别" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in s_case_typeOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="协办人:" prop="sOwnerId">
          <el-select v-model="formData.sOwnerId" placeholder="请选择协办人" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in dataSource.sOwnerId" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="类型:" prop="subdivide">
          <el-select v-model="formData.subdivide" placeholder="请选择类型" style="width:100%" :clearable="true">
            <el-option v-for="(item,key) in case_subdivideOptions" :key="key" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="录入状态:" prop="state">
          <el-input v-model.number="formData.state" :clearable="true" placeholder="请输入"/>
        </el-form-item>
        <el-form-item label="发案地址:" prop="address">
          <el-input v-model="formData.address" :clearable="true" placeholder="请输入发案地址"/>
        </el-form-item>
        <el-form-item label="简要案情:" prop="content">
          <RichEdit v-model="formData.content"/>
        </el-form-item>
        <el-form-item>
          <el-button :loading="btnLoading" type="primary" @click="save">保存</el-button>
          <el-button type="primary" @click="back">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import {createCasCases, findCasCases, getCasCasesDataSource, updateCasCases} from '@/api/cases/casCases'
// 自动获取字典
import {getDictFunc} from '@/utils/format'
import {useRoute, useRouter} from "vue-router"
import {ElMessage} from 'element-plus'
import {reactive, ref} from 'vue'
// 富文本组件
import RichEdit from '@/components/richtext/rich-edit.vue'

defineOptions({
  name: 'CasCasesForm'
})


const route = useRoute()
const router = useRouter()

// 提交按钮loading
const btnLoading = ref(false)

const type = ref('')
const townOptions = ref([])
const f_case_typeOptions = ref([])
const s_case_typeOptions = ref([])
const case_stateOptions = ref([])
const case_subdivideOptions = ref([])
const formData = ref({
  touchAt: new Date(),
  caseNo: '',
  caseName: '',
  townCode: '',
  fOwnerId: undefined,
  amount: undefined,
  caseState: '',
  fCaseType: '',
  sCaseType: '',
  subdivide: '',
  sOwnerId: undefined,
  state: undefined,
  content: '',
  address: '',
})
// 验证规则
const rule = reactive({
  touchAt: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  caseName: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  townCode: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  fOwnerId: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  amount: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  fCaseType: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  sCaseType: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  subdivide: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  },
    {
      whitespace: true,
      message: '不能只输入空格',
      trigger: ['input', 'blur'],
    }
  ],
  content: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
  address: [{
    required: true,
    message: '',
    trigger: ['input', 'blur'],
  }],
})

const elFormRef = ref()
const dataSource = ref([])
const getDataSourceFunc = async () => {
  const res = await getCasCasesDataSource()
  if (res.code === 0) {
    dataSource.value = res.data
  }
}
getDataSourceFunc()

// 初始化方法
const init = async () => {
  // 建议通过url传参获取目标数据ID 调用 find方法进行查询数据操作 从而决定本页面是create还是update 以下为id作为url参数示例
  if (route.query.id) {
    const res = await findCasCases({ID: route.query.id})
    if (res.code === 0) {
      formData.value = res.data
      type.value = 'update'
    }
  } else {
    type.value = 'create'
  }
  townOptions.value = await getDictFunc('town')
  f_case_typeOptions.value = await getDictFunc('f_case_type')
  s_case_typeOptions.value = await getDictFunc('s_case_type')
  case_stateOptions.value = await getDictFunc('case_state')
  case_subdivideOptions.value = await getDictFunc('case_subdivide')
}

init()
// 保存按钮
const save = async () => {
  btnLoading.value = true
  elFormRef.value?.validate(async (valid) => {
    if (!valid) return btnLoading.value = false
    let res
    switch (type.value) {
      case 'create':
        res = await createCasCases(formData.value)
        break
      case 'update':
        res = await updateCasCases(formData.value)
        break
      default:
        res = await createCasCases(formData.value)
        break
    }
    btnLoading.value = false
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '创建/更改成功'
      })
    }
  })
}

// 返回按钮
const back = () => {
  router.go(-1)
}

</script>

<style>
</style>
